<template>
  <div class="about">
    <h1>手写深拷贝</h1>
  </div>
</template>
<script setup lang="ts">
let obj1 = {
  name: 'heyongtao',
  adress: {
    city: '深圳'
  }
}
let obj2 = deepClone(obj1)
obj2.name = '123'
console.log(obj1, 'obj1')

function deepClone(data: any) {
  // 判断传进来的参数是否是数组或对象
  if (typeof data !== 'object' || data === null) {
    return data
  }

  // 判断传进来的是对象还是数组
  let res = Array.isArray(data) ? [] : {}
  for (const key in data) {
    if (data.hasOwnProperty(key)) {
      res[key] = data[key]
    }
  }
  return res
}
</script>
<style></style>
